package com.ysd.lis.controller.inv;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.inv.InvInstrManage;
import com.ysd.lis.service.inv.InvInstrManageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 仪器仓库 前端控制器
 * </p>
 *
 * @author WH
 * @since 2024-09-12
 */
@RestController
@RequestMapping("/inv/instrManage")
public class InvInstrManageController extends BaseController {

    @Autowired
    private InvInstrManageService invInstrManageService;

    @ApiOperation("根据仓库获取用户")
    @GetMapping("/findInstrManageList")
    public Result findInstrManageList(InvInstrManage invInstrManage) {

        return invInstrManageService.findInstrManageList(invInstrManage);
    }

    @ApiOperation("批量新增或修改")
    @PostMapping("/saveOrUpdateInstrManage")
    public Result saveOrUpdateInstrManage(@RequestBody List<InvInstrManage> invInstrManages) {
        Set<String> nameSet = new HashSet<>();
        List<InvInstrManage> instrManageList = new ArrayList<>();
        for (InvInstrManage instrManage : invInstrManages) {
            if (!nameSet.contains(instrManage.getInstrCode()) && StringUtils.isNotBlank(instrManage.getInstrCode())) {
                nameSet.add(instrManage.getInstrCode());
                instrManageList.add(instrManage);
            }
        }

        boolean b = invInstrManageService.saveOrUpdateBatch(instrManageList);
        if (b) {
            return Result.succ(1, "操作成功", null);
        } else {
            return Result.fail(400, "操作失败", "");
        }
    }

    @ApiOperation("批量删除")
    @PostMapping("/deleteInstrManage")
    public Result deleteInstrManage(@RequestBody List<InvInstrManage> invInstrManages) {
        List<String> ids = invInstrManages.stream().filter(item -> StringUtils.isNotBlank(item.getId())).map(BaseEntity::getId).collect(Collectors.toList());
        boolean b = invInstrManageService.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.succ(400, "删除失败", "");
        }
    }


}
